<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-04-11
 * Time: 14:40
 */
namespace app\api\common;
use think\Controller;
use think\Db;
use think\Exception;

class Commodity extends Controller
{
    protected static $err_code = 0;

    /**
     *
     * @apiGroup Commodity
     * @apiVersion 1.0.1
     * @apiDescription 好物
     * @api {post} /Commodity.index 好物
     * @apiName Commodity_index
     * @apiParam {int} page 页码
     *
     * @apiSuccess {int} code 结果码
     * @apiSuccess {String} msg 消息说明
     * @apiSuccess {Object} data
     * @apiSuccess {int} data.next_page  下一页页码
     * @apiSuccess {Object} data.data
     * @apiSuccess {int} data.data.good_id 好物id
     * @apiSuccess {String} data.data.title 好物标题
     * @apiSuccess {String} data.data.price 好物淘宝原价
     * @apiSuccess {String} data.data.itemid  好物淘宝id
     * @apiSuccess {float} data.data.desc 	好物推荐理由
     * @apiSuccess {int} data.data.shoptype 天猫/淘宝类型：天猫（B） 普通淘宝店铺（C）
     * @apiSuccess {float} data.data.coupon 是否有优惠券 0没有 1有
     * @apiSuccess {float} data.data.coupon_url 优惠券链接
     * @apiSuccess {float} data.data.coupon_money 优惠券金额
     * @apiSuccess {float} data.data.itemendprice 好物券后价
     * @apiSuccess {float} data.data.sell_counter 好物月销量
     * @apiSuccess {float} data.data.itempic 好物图片
     * @apiSuccess {String} data.data.share_url 分享的链接
     * @apiSuccessExample 成功示例
     *  HTTP/1.1 200 OK
     *  {
     *     code:200,
     *     msg:'success',
     *     data:{
     *         next_page:''
     *          data:{
    ...
     *              }
     *	   }
     *  }
     *
     *  @apiError code 0 系统异常错误
     *  @apiError msg token已经过期
     *  @apiErrorExample {json} 失败示例
     *  HTTP/1.1 404 Not Found
     *  {
     *     code:0,
     *      msg:'失败或没有数据返回',
     *   }
     */

    public static function index($param){
        try{
            $data_list = Db::table('yf_good')
                ->field('good_id,title,price,itemid,desc,shoptype,coupon,coupon_url,coupon_price coupon_money,itemendprice,itempic,sell_counter,begin_time couponstarttime,end_time couponendtime')
                ->where(['begin_time'=>['lt',time()],'end_time'=>['gt',time()]])
                ->order(['sort'=>'desc','good_id'=>'desc'])->paginate(10);
            $data_list = $data_list->toArray();
            if (!empty($data_list['data'])){
                foreach ($data_list['data'] as $k=>$v){
                    $itempic_list = explode(',',$v['itempic']);
                    foreach ($itempic_list as $key=>$val){
                        if ($key==0){
                            $data_list['data'][$k]['itempic'] = get_img_url($val);
                        }
                        $itempic_list[$key] = get_img_url($val);
                    }
                    $data_list['data'][$k]['itempic_list'] = $itempic_list;
                    $data_list['data'][$k]['share_url'] = '';
                }
            }
            return ['code'=>200,'msg'=>'SUCCESS','data'=>$data_list];
        }catch (Exception $e){
            return ['code' => self::$err_code, 'msg' => $e->getMessage()];
        }
    }

    public static function indexold($param){
        try{
            $data_list = Db::table('yf_good')
                ->field('good_id,title,price,itemid,desc,shoptype,coupon,coupon_url,coupon_price coupon_money,itemendprice,itempic,sell_counter')
                ->where(['begin_time'=>['lt',time()],'end_time'=>['gt',time()]])
                ->order(['sort'=>'desc','good_id'=>'desc'])->paginate(10);
            $data_list = $data_list->toArray();
            if (!empty($data_list['data'])){
                foreach ($data_list['data'] as $k=>$v){
                    $data_list['data'][$k]['itempic'] = get_img_url($v['itempic']);
                }
            }

            return ['code'=>200,'msg'=>'SUCCESS','data'=>$data_list];
        }catch (Exception $e){
            return ['code' => self::$err_code, 'msg' => $e->getMessage()];
        }

    }


    /**
     *
     * @apiGroup Commodity
     * @apiVersion 1.0.1
     * @apiDescription 好货列表新
     * @api {post} /Commodity.index_v2 好货列表新
     * @apiName Commodity_index_v2
     * @apiParam {int} page 页码
     *
     * @apiSuccess {int} code 结果码
     * @apiSuccess {String} msg 消息说明
     * @apiSuccess {Object} data
     * @apiSuccess {int} data.next_page  下一页页码
     * @apiSuccess {Object} data.data
     * @apiSuccess {int} data.data.good_id 好物id
     * @apiSuccess {String} data.data.itemtitle 好物标题
     * @apiSuccess {String} data.data.itemprice 好物淘宝原价
     * @apiSuccess {String} data.data.itemid  好物淘宝id
     * @apiSuccess {float} data.data.desc 	好物推荐理由
     * @apiSuccess {int} data.data.shoptype 天猫/淘宝类型：天猫（B） 普通淘宝店铺（C）
     * @apiSuccess {float} data.data.coupon 是否有优惠券 0没有 1有
     * @apiSuccess {float} data.data.couponurl 优惠券链接
     * @apiSuccess {float} data.data.couponmoney 优惠券金额
     * @apiSuccess {float} data.data.itemendprice 好物券后价
     * @apiSuccess {float} data.data.itemsale 好物月销量
     * @apiSuccess {float} data.data.itempic 好物封面图
     * @apiSuccess {int} data.data.couponstarttime 好货优惠券开始时间
     * @apiSuccess {int} data.data.couponendtime 好货优惠券结束时间
     * @apiSuccess {Object} data.data.itempic_list 好货图片列表
     * @apiSuccessExample 成功示例
     *  HTTP/1.1 200 OK
     *  {
     *     code:200,
     *     msg:'success',
     *     data:{
     *         next_page:''
     *          data:{
    ...
     *              }
     *	   }
     *  }
     *
     *  @apiError code 0 系统异常错误
     *  @apiError msg token已经过期
     *  @apiErrorExample {json} 失败示例
     *  HTTP/1.1 404 Not Found
     *  {
     *     code:0,
     *      msg:'失败或没有数据返回',
     *   }
     */

    public static function index_v2($param){
        try{
            $data_list = Db::table('yf_good')
                ->field('itemid,title itemtitle,price itemprice,itemendprice,itempic,coupon_url couponurl,shoptype,sell_counter itemsale,coupon_price couponmoney,begin_time couponstarttime,end_time couponendtime,good_id,desc,coupon')
                ->where(['begin_time'=>['lt',time()],'end_time'=>['gt',time()]])
                ->order(['sort'=>'desc','good_id'=>'desc'])->paginate(10);
            $data_list = $data_list->toArray();
            if (!empty($data_list['data'])){
                foreach ($data_list['data'] as $k=>$v){
                    $itempic_list = explode(',',$v['itempic']);
                    foreach ($itempic_list as $key=>$val){
                        if ($key==0){
                            $data_list['data'][$k]['itempic'] = get_img_url($val);
                        }
                        $itempic_list[$key] = get_img_url($val);
                    }
                    $data_list['data'][$k]['itempic_list'] = $itempic_list;
                }
            }
            return ['code'=>200,'msg'=>'SUCCESS','data'=>$data_list];
        }catch (Exception $e){
            return ['code' => self::$err_code, 'msg' => $e->getMessage()];
        }
    }


    /**
     *
     * @apiGroup Commodity
     * @apiVersion 1.0.1
     * @apiDescription 好货商品详情
     * @api {post} /Commodity.goods_detail 好货商品详情
     * @apiName Commodity_goods_detail
     * @apiParam {int} good_id 好货商品id
     *
     * @apiSuccess {int} code 结果码
     * @apiSuccess {String} msg 消息说明
     * @apiSuccess {Object} data
     * @apiSuccess {int} data.good_id 好货id
     * @apiSuccess {String} data.itemtitle 好货标题
     * @apiSuccess {String} data.itemprice 好货在售价
     * @apiSuccess {String} data.itemid  好货淘宝id
     * @apiSuccess {String} data.desc 	好货推荐理由
     * @apiSuccess {String} data.shoptype 天猫/淘宝类型：天猫（B） 普通淘宝店铺（C）
     * @apiSuccess {int} data.coupon 是否有优惠券 0没有 1有
     * @apiSuccess {String} data.coupon_url 优惠券链接
     * @apiSuccess {String} data.couponmoney 优惠券金额
     * @apiSuccess {String} data.itemendprice 好货券后价
     * @apiSuccess {int} data.itemsale 好物月销量
     * @apiSuccess {int} data.couponstarttime 好货优惠券开始时间
     * @apiSuccess {int} data.couponendtime 好货优惠券结束时间
     * @apiSuccess {String} data.share_url 分享的链接
     * @apiSuccess {Object} data.data.itempic_list 好货图片列表
     * @apiSuccessExample 成功示例
     *  HTTP/1.1 200 OK
     *  {
     *     code:200,
     *     msg:'success',
     *     data:{
     *         {
    ...
     *	   }
     *  }
     *
     *  @apiError code 0 系统异常错误
     *  @apiError msg token已经过期
     *  @apiErrorExample {json} 失败示例
     *  HTTP/1.1 404 Not Found
     *  {
     *     code:0,
     *      msg:'失败或没有数据返回',
     *   }
     */
    public static function goods_detail($param){
        if (empty($param['itemid'])) {
            return ['data' => '', 'code' => 400001, 'msg' => '参数错误'];
        }
        try{
            $data_list = Db::table('yf_good')
                ->field('good_id,title itemtitle,price itemprice,itemid,desc,shoptype,coupon,coupon_url couponurl,coupon_price couponmoney,itemendprice,itempic,sell_counter itemsale,begin_time couponstarttime,end_time couponendtime')
                ->where(['itemid'=>$param['itemid']])
                ->find();
            if (empty($data_list)){
                throw new Exception('无该商品信息');
            }
            if (!empty($data_list['itempic'])){
                $data_list['itempic_list'] = explode(",", $data_list['itempic']);
                $data_list['share_url'] = '';
                foreach ($data_list['itempic_list'] as $k=>$v){
                    $data_list['itempic_list'][$k] = get_img_url($v);
                }
            }
            return ['code'=>200,'msg'=>'SUCCESS','data'=>$data_list];
        }catch (Exception $e){
            return ['code' => self::$err_code, 'msg' => $e->getMessage()];
        }
    }

    /**
     *
     * @apiGroup Commodity
     * @apiVersion 2.0.0
     * @apiDescription 好货活动商品列表
     * @api {post} /commodity.activity_goods 好货活动商品列表
     * @apiName Commodity_activity_goods
     * @apiParam {int} activity_type 活动类型 0：今日上新 1：9.9特价 2:30元封顶 3：聚划算
     * @apiParam {int} page 页码
     *
     * @apiSuccess {int} code 结果码
     * @apiSuccess {String} msg 消息说明
     * @apiSuccess {Object} data
     * @apiSuccess {int} data.next_page  下一页页码
     * @apiSuccess {Object} data.data
     * @apiSuccess {int} data.data.good_id 好物id
     * @apiSuccess {String} data.data.title 好物标题
     * @apiSuccess {String} data.data.price 好物淘宝原价
     * @apiSuccess {String} data.data.itemid  好物淘宝id
     * @apiSuccess {float} data.data.desc 	好物推荐理由
     * @apiSuccess {int} data.data.shoptype 天猫/淘宝类型：天猫（B） 普通淘宝店铺（C）
     * @apiSuccess {float} data.data.coupon 是否有优惠券 0没有 1有
     * @apiSuccess {float} data.data.coupon_url 优惠券链接
     * @apiSuccess {float} data.data.coupon_money 优惠券金额
     * @apiSuccess {float} data.data.itemendprice 好物券后价
     * @apiSuccess {float} data.data.sell_counter 好物月销量
     * @apiSuccess {float} data.data.itempic 好物图片
     * @apiSuccess {String} data.data.share_url 分享的链接
     * @apiSuccessExample 成功示例
     *  HTTP/1.1 200 OK
     *  {
     *     code:200,
     *     msg:'success',
     *     data:{
     *         next_page:''
     *          data:{
    ...
     *              }
     *	   }
     *  }
     *
     *  @apiError code 0 系统异常错误
     *  @apiError msg token已经过期
     *  @apiErrorExample {json} 失败示例
     *  HTTP/1.1 404 Not Found
     *  {
     *     code:0,
     *      msg:'失败或没有数据返回',
     *   }
     */
    public static function activity_goods($param){
        if (!isset($param['activity_type'])) {
            return ['data' => '', 'code' => 400001, 'msg' => '参数错误'];
        }
        try{
            $activity_type = empty($param['activity_type'])? 0:$param['activity_type'];
            if ($activity_type == 0){
                $where['add_time'] = ['between',[strtotime(date('Ymd')),strtotime(date('Ymd'))+86400]]; //当天0:00-24:00
                $where['begin_time'] = ['lt',time()]; //展示开始时间
                $where['end_time'] = ['gt',time()]; //展示结束时间
            }else{
                $where['begin_time'] = ['lt',time()]; //展示开始时间
                $where['end_time'] = ['gt',time()]; //展示结束时间
                $where[] = array('exp',"FIND_IN_SET('$activity_type',activity_type)");
            }
            $data_list = Db::table('yf_good')
                ->field('itemid,title itemtitle,price itemprice,itemendprice,itempic,coupon_url couponurl,shoptype,sell_counter itemsale,coupon_price couponmoney,begin_time couponstarttime,end_time couponendtime,good_id,desc,coupon')
                ->where($where)
                ->order(['sort'=>'desc','good_id'=>'desc'])->paginate(10);
            $data_list = $data_list->toArray();
            if (!empty($data_list['data'])){
                foreach ($data_list['data'] as $k=>$v){
                    $itempic_list = explode(',',$v['itempic']);
                    foreach ($itempic_list as $key=>$val){
                        if ($key==0){
                            $data_list['data'][$k]['itempic'] = get_img_url($val);
                        }
                        $itempic_list[$key] = get_img_url($val);
                    }
                    $data_list['data'][$k]['itempic_list'] = $itempic_list;
                    $data_list['data'][$k]['share_url'] = '';
                }
            }
            return ['code'=>200,'msg'=>'SUCCESS','data'=>$data_list];
        }catch (Exception $e){
            return ['code' => self::$err_code, 'msg' => $e->getMessage()];
        }
    }

}
